import isEmail from 'validator/lib/isEmail';
import { getData, setData } from './dataStore';
import { Error, users, Success, QuizList } from './interface';
import { isValidNameContains, isValidToken, isValidNameLength, isValidPasswordContains, isValidPasswordLength } from './check';

export function adminUserDetailsUpdate( 
    token : string,
    email: string,
    nameFirst: string,
    nameLast: string
): Success | Error {
    const data = getData();

    const authUserId = isValidToken(token);
    if (!authUserId) {
        return { error: 'Token is empty or invalid (does not refer to valid logged in user session)' };
    }

    if (data.users.some((user: users) => user.email === email && user.userID !== authUserId)) {
        return { error: 'Email is currently used by another user (excluding the current authorised user)' };
    }

    if (!isEmail(email)) {
        return { error: 'Email does not satisfy.' };
    }

    if (!isValidNameLength(nameFirst)) {
        return { error: 'NameFirst is less than 2 characters or more than 20 characters.' };
    }

    if (!isValidNameContains(nameFirst)) {
        return { error: 'NameFirst contains characters other than lowercase letters, uppercase letters, spaces, hyphens, or apostrophes.' };
    }

    if (!isValidNameLength(nameLast)) {
        return { error: 'NameLast is less than 2 characters or more than 20 characters.' };
    }

    if (!isValidNameContains(nameLast)) {
        return { error: 'NameLast contains characters other than lowercase letters, uppercase letters, spaces, hyphens, or apostrophes.' };
    }

    const targetIndex = data.users.findIndex((user: users) => user.userID === authUserId);

    if (targetIndex !== -1) {
        data.users[targetIndex].FirstName = nameFirst;
        data.users[targetIndex].LastName = nameLast;
        data.users[targetIndex].email = email;
    }

    setData(data);
    return { message: 'Success!' };
}

export function adminUserPasswordUpdate(
    token: string,
    oldPassword: string,
    newPassword: string
): Success | Error {
    const data = getData();
    
    const authUserId = isValidToken(token);
    if (!authUserId) {
        return { error: 'Token is empty or invalid (does not refer to valid logged in user session)' };
    }

    const targetIndex = data.users.findIndex((user: users) => user.userID === authUserId);
    if (targetIndex !== -1) {
        if (data.users[targetIndex].password  !== oldPassword) {
            return { error: 'Old Password is not the correct old password.' };
        }

        if (data.users[targetIndex].password === newPassword) {
            return { error: 'Old Password and New Password match exactly.' };
        }

        const passwordExists = data.users[targetIndex].usedPasswords.some(usedPassword => usedPassword === newPassword);
        if (passwordExists) {
            return { error: "New Password has already been used before by this user." };
        }
    }

    if (!isValidPasswordLength(newPassword)) {
        return { error: 'Password is less than 8 characters.' };
    }

    if (!isValidPasswordContains(newPassword)) {
        return { error: 'Password does not contain at least one number and at least one letter.' };
    }
    

    data.users[targetIndex].usedPasswords.push(oldPassword);
    data.users[targetIndex].password = newPassword;
    setData(data);
    
    return { message: 'Success!' };
}